package com.zeta.demo.es.model.document;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 文章表 ElasticSearch文档
 *
 * @author gcc
 */
@Schema(description = "文章")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Document(indexName = "post")
public class PostDocument {

    /** 文章id 使用string类型 避免雪花id在es中丢失精度 */
    @Id
    @Field(type = FieldType.Keyword)
    @Schema(description = "主键")
    private String id;

    /** 文章标题，主字段(text+ik分词)用于搜索，子字段(keyword)用于高亮 */
    @MultiField(
       mainField = @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer ="ik_smart"),
       otherFields = {
           @InnerField(suffix = "keyword", type = FieldType.Keyword)
       }
    )
    @Schema(description = "标题")
    private String title;

    /** 文章内容，主字段(text+ik分词)用于搜索，子字段(keyword)用于高亮 */
    @MultiField(
        mainField = @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer ="ik_smart"),
        otherFields = {
            @InnerField(suffix = "keyword", type = FieldType.Keyword)
        }
    )
    @Schema(description = "内容")
    private String content;

    /** 作者名，用于精确过滤和聚合 */
    @Field(type = FieldType.Keyword)
    @Schema(description = "作者")
    private String author;

    /** 创建时间 */
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    @Schema(description = "创建时间")
    private LocalDateTime createTime;

    /** 发布时间 */
    @Field(type = FieldType.Date, format = DateFormat.date_hour_minute_second)
    @Schema(description = "发布时间")
    private LocalDateTime publishTime;

    /** 标签列表，非常适合用keyword类型 */
    @Field(type = FieldType.Keyword)
    @Schema(description = "标签")
    private List<String> tags;

    /** 测试字段 说明：测试LocalDate转换有没有问题 */
    @Field(type = FieldType.Date, format = DateFormat.date)
    @Schema(description = "测试字段", hidden = true)
    private LocalDate testField;

}
